package com.micjam.calculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CalculatorActivity extends Activity {

	private String currentInput;
	private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9,
			btnDel, btnAc, btnMulti, btnDivi, btnAddi, btnSubt,
			btnBrackedStart, btnBrackedEnd, btnResult, btnDecimalPoint;
	private TextView tvResult, tvOperation;
	private int bracketOpened = 0;
	private double[] storedResult = { 0, 0 };
	private char[] operator = { '=', 'e' };
	private boolean res = false, a = true;

	// private String[] num = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
	// "0"};
	// private String[] other = {"Ac", "Del"};
	// private String[] operation = {"+", "-", "*", "/"};
	// private String[] bracket = {"(", ")"};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculator);
		currentInput = "";
		tvResult = (TextView) findViewById(R.id.tvResult);
		tvOperation = (TextView) findViewById(R.id.tvOperation);
		btn0 = (Button) findViewById(R.id.btn0);
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn3 = (Button) findViewById(R.id.btn3);
		btn4 = (Button) findViewById(R.id.btn4);
		btn5 = (Button) findViewById(R.id.btn5);
		btn6 = (Button) findViewById(R.id.btn6);
		btn7 = (Button) findViewById(R.id.btn7);
		btn8 = (Button) findViewById(R.id.btn8);
		btn9 = (Button) findViewById(R.id.btn9);
		btnDel = (Button) findViewById(R.id.btnDel);
		btnAc = (Button) findViewById(R.id.btnAc);
		btnMulti = (Button) findViewById(R.id.btnMulti);
		btnDivi = (Button) findViewById(R.id.btnDivi);
		btnAddi = (Button) findViewById(R.id.btnAddi);
		btnSubt = (Button) findViewById(R.id.btnSubt);
		btnDecimalPoint = (Button) findViewById(R.id.btnDecimalPoint);
		btnResult = (Button) findViewById(R.id.btnResult);

		tvResult.setText(String.valueOf(storedResult[0]));

		btn0.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("0");
			}
		});
		btn1.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("1");
			}
		});
		btn2.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("2");
			}
		});
		btn3.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("3");
			}
		});
		btn4.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("4");
			}
		});
		btn5.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("5");
			}
		});
		btn6.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("6");
			}
		});
		btn7.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("7");
			}
		});
		btn8.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("8");
			}
		});
		btn9.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber("9");
			}
		});
		btnDel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				delNumber();
			}
		});
		btnAc.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				clearAll();
			}
		});
		btnMulti.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addOperator('*');
				tvOperation.setText("*");
			}
		});
		btnDivi.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addOperator('/');
				tvOperation.setText("/");
			}
		});
		btnAddi.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addOperator('+');
				tvOperation.setText("+");
			}
		});
		btnSubt.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addOperator('-');
				tvOperation.setText("-");
			}
		});

		btnDecimalPoint.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addNumber(".");
			}
		});
		btnResult.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				addOperator('=');
			}
		});
	}

	protected void clearAll() {
		currentInput = "";
		storedResult[0] = 0;
		tvResult.setText(String.valueOf(storedResult[0]));
		tvOperation.setText("");
		operator[0] = '=';
		operator[1] = 'e';
		res = false;
		a = true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_calculator, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.itemAbout:
			Intent aboutIntent = new Intent(CalculatorActivity.this, AboutActivity.class);
			CalculatorActivity.this.startActivity(aboutIntent);
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void addNumber(String str) {
		if (a && currentInput.length() < 10) {
			currentInput += str;
			tvResult.setText(currentInput);
			tvOperation.setText("");
			res = false;
		}
	}

	private void delNumber() {
		if (a) {
			if (currentInput.length() > 0) {
				currentInput = currentInput.substring(0,
						currentInput.length() - 1);
				if (currentInput.length() == 0)
					currentInput = "";
				tvResult.setText(currentInput);
			}
		}
	}

	private void addOperator(char str) {
		if (str == '=') { // =
			res = false;
			if (currentInput != "") {
				if (operator[0] == '+') {
					addition();
				} else if (operator[0] == '-') {
					subtraction();
				} else if (operator[0] == '*') {
					multiplication();
				} else if (operator[0] == '/') {
					division();
				} else if (operator[0] == '=') {
					result();
				}
				a = false;
			}
			res = true;
		} else if (str == '+') { // +
			if (currentInput != "") {
				if (operator[0] == '+') {
					addition();
				} else if (operator[0] == '-') {
					subtraction();
				} else if (operator[0] == '*') {
					multiplication();
				} else if (operator[0] == '/') {
					division();
				} else if (operator[0] == '=') {
					result();
				}
				operator[1] = operator[0];
			}
			a = true;
			operator[0] = '+';
			currentInput = "";
		} else if (str == '-') { // -
			if (currentInput != "") {
				if (operator[0] == '+') {
					addition();
				} else if (operator[0] == '-') {
					subtraction();
				} else if (operator[0] == '*') {
					multiplication();
				} else if (operator[0] == '/') {
					division();
				} else if (operator[0] == '=') {
					result();
				}
				operator[1] = operator[0];
			}
			a = true;
			operator[0] = '-';
			currentInput = "";
		} else if (str == '*') { // *
			if (currentInput != "") {
				if (operator[0] == '+' || operator[0] == '-') {
					storedResult[1] = storedResult[0];
					storedResult[0] = Double.valueOf(currentInput);
					operator[1] = operator[0];
				} else if (operator[0] == '*') {
					multiplication();
				} else if (operator[0] == '/') {
					division();
				} else if (operator[0] == '=') {
					result();
				}
				operator[1] = operator[0];
			}
			a = true;
			operator[0] = '*';
			currentInput = "";
		} else if (str == '/') { // /
			if (currentInput != "") {
				if (operator[0] == '+' || operator[0] == '-') {
					storedResult[1] = storedResult[0];
					storedResult[0] = Double.valueOf(currentInput);
					operator[1] = operator[0];
				} else if (operator[0] == '*') {
					multiplication();
				} else if (operator[0] == '/') {
					division();
				} else if (operator[0] == '=') {
					result();
				}
				operator[1] = operator[0];
			}
			a = true;
			operator[0] = '/';
			currentInput = "";
		}
	}

	private void result() {
		storedResult[0] = Double.valueOf(currentInput);
		tvResult.setText(String.valueOf(storedResult[0]));
		tvOperation.setText("");
	}

	private void subtraction() {
		if (!res) {
			storedResult[0] -= Double.valueOf(currentInput);
			tvResult.setText(String.valueOf(storedResult[0]));
		}
	}

	private void addition() {
		if (!res) {
			storedResult[0] += Double.valueOf(currentInput);
			tvResult.setText(String.valueOf(storedResult[0]));
		}
	}

	private void multiplication() {
		if (!res) {
			storedResult[0] *= Double.valueOf(currentInput);
			if (operator[1] == '+') {
				storedResult[0] += storedResult[1];
			} else if (operator[1] == '-') {
				storedResult[0] = storedResult[1] - storedResult[0];
			}
			tvResult.setText(String.valueOf(storedResult[0]));
		}
	}

	private void division() {
		if (!res) {
			storedResult[0] /= Double.valueOf(currentInput);
			if (operator[1] == '+') {
				storedResult[0] += storedResult[1];
			} else if (operator[1] == '-') {
				storedResult[0] = storedResult[1] - storedResult[0];
			}
			tvResult.setText(String.valueOf(storedResult[0]));
		}
	}
}
